/**
 * Created by kevin on 2017/01/08.
 *
 * page base
 */


var eosi = window.eosi || {};
this.eosi.website = this.eosi.website || {};

(function (window) {
    // the application


    function BgLayer() {

        this.Container_constructor();
        this.init();
        //console.log(this.father.targetWidth);

    }


    var p = createjs.extend(BgLayer, createjs.Container);
    p.className = "BgLayer";
    //背景
    p.bg;
    //加载器
    p.loader;

    //背景颜色
    p.bgColor = "#ff0000";

    p.config;

    // public methods:
    p.init = function () {
        this.loadRes();
        //console.log(this.className);
    }


    /*
     * 加载资源
     * */
    p.loadRes = function(){
        this.loader = new createjs.LoadQueue(true);
        this.loader.on("complete", this.loadComplete, this);
        this.loader.on("fileload", this.onLoaded, this);
        /*if(this.manifest != undefined){
            this.loader.loadManifest(this.manifest, true, "./assets/");
        }else{
            this.loader.loadManifest({src: this.resObj.path + "?v="+version, callback: this.resObj.callback, type: "manifest"}, true, "./assets/");
        }*/
        this.loader.loadManifest({src: BASE_PATH + "static/BgLayerManifest.json?v="+version, callback: "loadBgMediaGrid", type: "manifest"}, true, "");

    }
    /*
    * 资源加载
    * */
    p.onLoaded = function (evt){
        //console.log(evt)
        var item = evt.item;

        if(item.id == "bglayer_bg"){
            //console.log(item);
            //this.drawBg();
            this.config = evt.item;
        }
    }
    /*
     * 资源加载完毕
     * */
    p.loadComplete = function (evt){
        if(this.config){
            this.drawBg();
        }

    }

    /*
     * 绘制背景
     * */
    p.drawBg = function(){
        //console.log(this.loader.manifest);
        //是否有背景图
        var hasBgPic = this.loader.getResult("bglayer_bg");
        if(hasBgPic){
            this.bg = new createjs.Bitmap(hasBgPic);
            //console.log(this.config.fit)
            switch (this.config.fit){
                case "fit_all"://强制适应
                    this.bg.scaleX = targetWidth/hasBgPic.width;
                    this.bg.scaleY = targetHeight/hasBgPic.height;
                    break;
                case  "fit_y"://竖适应，横形变根据竖
                    //console.log(targetHeight/hasBgPic.height)
                    this.bg.scaleY = targetHeight/hasBgPic.height;
                    this.bg.scaleX = this.bg.scaleY;
                    break;
                case  "fit_x"://横适应，竖形变根据横
                    this.bg.scaleX = targetWidth/hasBgPic.width;
                    this.bg.scaleY = this.bg.scaleX;
                    break;
                default :
                    this.bg.scaleX = targetWidth/hasBgPic.width;
                    this.bg.scaleY = targetHeight/hasBgPic.height;
                    break;
            }
            switch (this.config.align){
                case "TL"://左上
                    this.bg.x = 0;
                    this.bg.y = 0;
                    break;
                case "TR"://右上
                    this.bg.x = targetWidth - hasBgPic.width * this.bg.scaleX;
                    this.bg.y = 0;
                    break;
                case "TM"://顶部＋左右居中
                    this.bg.x = (targetWidth - hasBgPic.width * this.bg.scaleX)*.5;
                    this.bg.y = 0;
                    break;
                case "BL"://左下
                    this.bg.x = 0;
                    this.bg.y = targetHeight - hasBgPic.height * this.bg.scaleY;
                    break;
                case "BR"://右下
                    this.bg.x = targetWidth - hasBgPic.width * this.bg.scaleX;
                    this.bg.y = targetHeight - hasBgPic.height * this.bg.scaleY;
                    break;
                case "BM"://底部＋左右居中
                    this.bg.x = (targetWidth - hasBgPic.width * this.bg.scaleX)*.5;
                    this.bg.y = targetHeight - hasBgPic.height * this.bg.scaleY;
                    break;
                case "MM"://左右上下都居中
                    this.bg.x = (targetWidth - hasBgPic.width * this.bg.scaleX)*.5;
                    this.bg.y = (targetHeight - hasBgPic.height * this.bg.scaleY)*.5;
                    break;
                default :
                    this.bg.x = (targetWidth - hasBgPic.width * this.bg.scaleX)*.5;
                    this.bg.y = (targetHeight - hasBgPic.height * this.bg.scaleY)*.5;
                    break;

            }
        }else{
            this.bg = new createjs.Shape();
            this.bg.graphics.beginFill(this.bgColor).drawRect(0,0,targetWidth,targetHeight).endFill();
        }

        this.addChild(this.bg);


    }




    // add MyApp to myNameSpace
    window.eosi.website.BgLayer = createjs.promote(BgLayer, "Container");
//}());
}(window));





